import axios from 'axios';
import qs from 'qs';
import router from 'vue-router';
import { Indicator } from 'mint-ui';
import store from '../../store/index'

//超时时间
axios.defaults.timeout = 5000;
var loadinginstace;
//http request 拦截器
axios.interceptors.request.use(
    config => {
        let token = window.localStorage.getItem("access_token");

        // const token = getCookie('名称');注意使用的时候需要引入cookie方法，推荐js-cookie
        config.headers = {

            'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
            'Content-Type': 'application/json;charset=UTF-8',
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        let page = store.state.page;
        if (page == 2) {
            config.headers = {
                'Authorization': 'Bearer ' + token,
                // 'Content-Type': 'application/json;charset=UTF-8',
                'Accept': 'application/json, text/plain, */*',
                'Content-Type': 'application/x-www-form-urlencoded',
            }
        }
        // Indicator.open({
        //   text: 'Loading...',
        //   spinnerType: 'fading-circle'
        // });

        // this.$openLoading();
        // console.log('进入request拦截器');

        // console.log('走这里',config);
        // loadinginstace =  Indicator.open({text: '加载中...',spinnerType: 'snake'});
        // loadinginstace = Indicator.service({ fullscreen: true });

        // if(token){
        //   config.params = {'token':token}
        // }
        // let token = window.localStorage.getItem('access_token');
        // if (token != null) {
        //     config.headers.Authorization = 'Bearer ' + token
        //         // config.headers.Authorization = 'Bearer ' + '4a0376af6dbfaa5d71fd92e064666752';
        // }
        return config;
    },
    err => {
        // Indicator.close();
        return Promise.reject(err);
    }
);


//http response 拦截器
axios.interceptors.response.use(
    response => {
        // console.log(response);
        Indicator.close();
        // if (response.data.Code == 4011) {
        //     console.log(VueRouter.pust());
        //     console.log('-----', response.data)
        //     Router.push({
        //         path: "/",
        //         querry: { redirect: Router.currentRoute.fullPath } //从哪个页面跳转
        //     })
        // }


        Indicator.close();
        // console.log('进入response拦截器');
        return response;
    },
    err => {
        Indicator.close();
        return Promise.reject(err);
    }
)



/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function fetch(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
                params: params
            })
            .then(response => {
                resolve(response.data)
            })
            .catch(err => {
                reject(err)
            })
    })
}


/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, params = {}) {
    console.log(qs.stringify(params));
    return new Promise((resolve, reject) => {
        axios.post(url, qs.stringify(params))
            .then(response => {
                resolve(response.data)
            }, err => {
                reject(err)
            })
    })
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function patch(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.patch(url, data)
            .then(response => {
                resolve(response.data)
            }, err => {
                reject(err)
            })
    })
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.put(url, data)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err)
            })
    })
}

export default axios